/*
 Copyright 2007 Razvan Taranu

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */
package core;

import generators.JavaHelper;
import generators.JavaJsonizers;
import generators.JavaProxies;
import generators.PhpBeans;
import generators.PhpHelper;
import generators.PhpJsonizers;
import generators.PhpServices;

import java.io.File;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thoughtworks.qdox.JavaDocBuilder;

public class Lacertae {
    private static Log log = LogFactory.getLog(Lacertae.class);
    private static Configuration config;
    private static JavaDocBuilder jdb;

    public static void main(String[] args) throws ConfigurationException {
        log.info("looking for configuration file default.properties in the classpath");
        config = new PropertiesConfiguration("default.properties");

        boolean error = false;

        // check these properties to make sure they are not blank
        String[] properties = { "java.source.path", "java.output.path", "php.output.path" };

        for (String prop : properties) {
            if (StringUtils.isBlank(config.getString(prop))) {
                error = true;
                log.error(prop + " is not defined");
            }
        }

        String javaSource = config.getString("java.source.path");
        File file = new File(javaSource);
        if (!file.exists() || !file.isDirectory()) {
            log.error("[" + file + "] is not a valid java source directory");
            error = true;
        }

        if (error) {
            System.exit(1);
        }

        log.info("loading the java source files");

        jdb = new JavaDocBuilder();
        jdb.addSourceTree(file);

        JavaHelper.configure(jdb, config);
        PhpHelper.configure(jdb, config);

        mainJavaJsonizers();
        mainJavaProxies();

        mainPhpBeans();
        mainPhpJsonizers();
        mainPhpServices();

        PhpHelper.copyTemplates();
    }

    private static void mainJavaJsonizers() {
        JavaJsonizers jsonizers = new JavaJsonizers();

        jsonizers.setBeansPackage(config.getString("java.beans.package"));
        jsonizers.setJsonizersPackage(config.getString("java.jsonizers.package"));

        jsonizers.run();
    }

    private static void mainJavaProxies() {
        JavaProxies proxies = new JavaProxies();

        proxies.setBeansPackage(config.getString("java.beans.package"));

        proxies.setJsonizersPackage(config.getString("java.jsonizers.package"));
        proxies.setProxiesPackage(config.getString("java.proxies.package"));

        proxies.run();
    }

    private static void mainPhpBeans() {
        PhpBeans beans = new PhpBeans();
        beans.run();
    }

    private static void mainPhpJsonizers() {
        PhpJsonizers jsonizers = new PhpJsonizers();
        jsonizers.run();
    }

    private static void mainPhpServices() {
        PhpServices service = new PhpServices();
        service.run();
    }
}
